package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.service.impl.DishServiceImpl;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@Slf4j
@Api(tags = "菜品相关接口")
@RequestMapping("/admin/dish")
public class DishController {

    @Autowired
    DishServiceImpl dishService;

    @Autowired
    RedisTemplate redisTemplate;
    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品接口")
    public Result addDish(@RequestBody DishDTO dishDTO) {
        dishService.addDish(dishDTO);

        String pattern="dish_"+dishDTO.getCategoryId();
        cleancache(pattern);

        return Result.success();
    }



    /**
     * 分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询接口")
    public Result<PageResult> getDishPage(DishPageQueryDTO dishPageQueryDTO){
        PageResult p= dishService.getDishPage(dishPageQueryDTO);
        return Result.success(p);
    }

   /* @GetMapping("/page")
    @ApiOperation("分页查询接口")
    public Result<PageResult> getDishPage(DishPageQueryDTO dishPageQueryDTO){
        PageResult p= dishService.getDishPage1(dishPageQueryDTO);
        return Result.success(p);
    }*/

    /**
     * 菜品批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除接口")
    public Result deleteByIds(@RequestParam List<Long> ids) {

   /*     log.info("ids-0:{}",ids.get(0));
        log.info("ids-1:{}",ids.get(1));
        log.info("ids-2:{}",ids.get(2));
*/
        dishService.deleteBach(ids);
        cleancache("dish_*");
        return Result.success();
    }

    /**
     * 根据id查询---->用于回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品接口")
    public Result<DishVO> getDishById(@PathVariable Long id) {
        DishVO dishVO=dishService.getDishById(id);
        return Result.success(dishVO);
    }


    /**
     * 修改菜品信息
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品接口")
    public Result updateDish(@RequestBody DishDTO dishDTO) {

        dishService.updateDish(dishDTO);
        cleancache("dish_*");
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<Dish[]> getDishList(Long categoryId){
        Dish[] dish  = dishService.getDishListBycategoryId(categoryId);
        return Result.success(dish);
    }


    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status,Long id){

        dishService.setStatus(status,id);
        cleancache("dish_*");
        return Result.success();
    }

    /**
     * 专门用来清除redis缓存
     * @param pattern
     */
    private void cleancache(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
